const { hoseadd, findhoseaddress, hoseseachkey, hosefindid, hoselove, allhoselove, leavehoselove, historyset, findhistroy, deletehistroy, allhistorys, findmyhose, updatahoseishas } = require('../../serves/hose/index')
module.exports = {
    async hoseadd(ctx, next) {
        const { type, title, erea, sum, context, tag, price, fx, address } = ctx.request.body
        const hosedata = {
            type,
            title,
            erea: Number(erea),
            sum,
            context,
            tag,
            price: Number(price),
            fx,
            address,
            img: ctx.request.files.img.newFilename,
            userid: ctx.id,
            ishas: true
        }
        try {
            const rudata = await hoseadd(hosedata)
            ctx.body = {
                code: 200,
                msg: "发布成功！"
            }
        } catch (error) {
            console.log(error)
        }
    },
    async hoseaddress(ctx, next) {
        const { address, type, sum, area, price, limit, offset } = ctx.request.body
        const rudata = await findhoseaddress({ address, type, sum, area, price, limit, offset })
        ctx.body = {
            code: 200,
            msg: "成功",
            data: rudata
        }
    },
    async hoseseach(ctx, next) {
        let { keyword, limit, offset } = ctx.query
        limit = Number(limit)
        offset = Number(offset)
        const rudata = await hoseseachkey(keyword, limit, offset)
        ctx.body = {
            code: 200,
            msg: "查询成功！",
            data: rudata
        }
    },
    async hoseid(ctx, next) {
        const { id } = ctx.params
        const rudata = await hosefindid(id)
        ctx.body = {
            code: 200,
            msg: "查询成功！",
            data: rudata
        }
    },
    async userlove(ctx, next) {
        const { id: hoseid } = ctx.params
        const userid = ctx.id
        const rudata = await hoselove(userid, hoseid)
        ctx.body = {
            code: 200,
            msg: "收藏成功！",
        }
    },
    async allloves(ctx, next) {
        const userid = ctx.id
        console.log(userid)
        const rudata = await allhoselove(userid)
        ctx.body = {
            code: 200,
            msg: "查询收藏成功！",
            data: rudata
        }
    },
    async leavelove(ctx, next) {
        const hoseid = ctx.params.hoseid
        const userid = ctx.params.userid
        const rudata = await leavehoselove(hoseid, userid)
        ctx.body = {
            code: 200,
            msg: "取消成功！",
        }
    },
    async sethistroy(ctx, next) {
        const hoseid = ctx.params.hoseid
        const uerid = ctx.id
        const finddata = await findhistroy(hoseid, uerid)
        if (!finddata) {
            console.log("没有")
            const rudata = await historyset(hoseid, uerid)
            ctx.body = {
                code: 200,
                msg: "ok",
            }
            return
        }
        console.log("有")
        await deletehistroy(finddata.id)
        await historyset(hoseid, uerid)
        ctx.body = {
            code: 200,
            msg: "ok",
        }
    },
    async allhistory(ctx, next) {
        const limit = +ctx.params.limit
        const offset = +ctx.params.offset
        const rudata = await allhistorys(ctx.id, limit, offset)
        console.log(ctx.id, 1111)
        return ctx.body = {
            code: 200,
            data: rudata
        }
    },
    async delehistroy(ctx, next) {
        const id = ctx.params.id
        await deletehistroy(id)
        ctx.body = {
            code: 200,
            msg: "删除成功！"
        }
    },
    async myselfhose(ctx, next) {
        const userid = ctx.id
        const rudata = await findmyhose(userid)
        ctx.body = {
            code: 200,
            msg: "查询成功！",
            data: rudata
        }
    },
    async okhose(ctx, next) {
        const userid = ctx.id
        const hoseid = ctx.params.hoseid
        const rudata = await updatahoseishas(userid, hoseid, 2)
        ctx.body = {
            code: 200,
            msg: "ok"
        }
    },
    async hosedown(ctx, next) {
        const userid = ctx.id
        const hoseid = ctx.params.hoseid
        const rudata = await updatahoseishas(userid, hoseid, 3)
        ctx.body = {
            code: 200,
            msg: "ok"
        }
    },
    async hoseup(ctx, next) {
        const userid = ctx.id
        const hoseid = ctx.params.hoseid
        const rudata = await updatahoseishas(userid, hoseid, 1)
        ctx.body = {
            code: 200,
            msg: "ok"
        }
    }
}
